package com.dal.remote;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.bean.AccountInfo;
import org.bean.FunctionSwitchDTO;
import org.bean.GameServerInfo;
import org.bean.InterceptInfoDTO;
import org.cache.FunctionSwitchCache;
import org.cache.InterceptMappingCache;
import org.error.LoginErrorCode;
import org.remote.login.RemoteLoginServer;

import com.dal.cache.GameServerInfoCache;
import com.dal.cache.TokenCache;
import com.dal.constant.AccountConstant;
import com.dal.db.DbSave;
import com.dal.db.bean.Account;
import com.dal.db.cache.AccountCache;
import com.dal.manager.AccountManager;
import com.dal.manager.TempRewardManager;
import com.google.common.collect.Lists;
import com.sojoys.artifact.annotation.RemoteServer;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.tools.ToolError;

/**
 * 登录RMI Created by fxf on 2017/6/27.
 */
@RemoteServer(RemoteLoginServer.class)
public class LoginRMIServerImpl implements RemoteLoginServer {
	@Override
	public void gameServerStart() {
		
	}

	@Override
	public void socialServerStart() {
	}

	@Override
	public AccountInfo loginVerify(String token) throws AbstractLogicModelException {
		String uid = TokenCache.getToken(token);
		
		ToolError.isAndTrue(LoginErrorCode.TOKEN_IS_OUT_OF_TIME,"token无效", uid==null);
		Account account = AccountCache.me().getById(uid);
		
		AccountInfo accountInfo = new AccountInfo();
//		ToolError.isAndTrue(LoginErrorCode.TOKEN_IS_OUT_OF_TIME,"token无效", true);
		
		accountInfo.setUid(account.getId());
		accountInfo.setChannelId(account.getChannelId());
		accountInfo.setChannelAppId(account.getChannelAppId());
		accountInfo.setDevice(account.getDeviceName());
		accountInfo.setOsType(account.getOsName());
		accountInfo.setOsVersion(account.getOsVersion());
		accountInfo.setClientVersion(account.getClientVersion());
		accountInfo.setImei(account.getDeviceId());
		accountInfo.setAdmin(account.isAdmin());
		accountInfo.setLanguage(account.getLanguage());
		accountInfo.setCreateDate(account.getCreateDate());
		accountInfo.setIp(account.getIp());
		accountInfo.setLastOnlineTime(account.getLastOnlineTime());
		accountInfo.setLastOfflineTime(account.getLastOfflineTime());
		accountInfo.setState(account.getState());
		accountInfo.setPhoneNumber(account.getPhoneNumber());
		accountInfo.setGameServerId(account.getGameServerId());
		// 线下奖励
		accountInfo.setTempRewards(TempRewardManager.me().getReward(account));
		return accountInfo;
	}

	@Override
	public void registerGameServerInfo(GameServerInfo gameServerInfo) throws AbstractLogicModelException {
		GameServerInfoCache.putCache(gameServerInfo);
	}

	@Override
	public void noticeIntercept(List<InterceptInfoDTO> list) {
		InterceptMappingCache.changeInterceptInfo(list);
//		AccountManager.changeAccountState(list);
	}
	
	public void noticeSwitch(List<FunctionSwitchDTO> list) {
		FunctionSwitchCache.changeCache(list);
	}


	@Override
	public List<Map> getAccountInfo(List<String> accountIdList, int channelId	) {
		List<Map> result = Lists.newArrayList();
		accountIdList.forEach(accountId ->{
			Account account = AccountCache.me().getByAccountIdChannelId(accountId,channelId);
			if (account == null) {
				return;
			}
			result.add(account.toMap(DataModelKeyEnum.SOURCE));
		});
		return result;
	}
	
	@Override
	public List<Map> getAccountInfo(List<String> uids) {
		List<Map> result = Lists.newArrayList();
		uids.forEach(uid ->{
			Account account = AccountCache.me().getById(uid);
			if (account == null) {
				return;
			}
			result.add(account.toMap(DataModelKeyEnum.SOURCE));
		});
		return result;
	}

	@Override
	public void online(String uid) {
		Account account = AccountCache.me().getById(uid);
		if (account == null){
			return;
		}
		account.setLastOnlineTime(new Date());
		account.setOnlineState(AccountConstant.ACCOUNT_STATE_ONLINE);
	}

	@Override
	public void offline(String uid) {
		Account account = AccountCache.me().getById(uid);
		if (account == null){
			return;
		}
		account.setLastOfflineTime(new Date());
		account.setOnlineState(AccountConstant.ACCOUNT_STATE_OFFLINE);

	}

	@Override
	public GameServerInfo getServerInfo(String serverId) {
		return GameServerInfoCache.getServerInfo(serverId);
	}

	@Override
	public List<GameServerInfo> getServers() {
		return GameServerInfoCache.getServerList();
	}

	@Override
	public boolean operateServer(int type, List<String> serverIds) {
		return GameServerInfoCache.operateServer(type, serverIds);
	}

	@Override
	public boolean operateAccount(int type, List<String> uids,Map paramMap) {
		AccountManager.operateAccount(type,uids,paramMap);
		return true;
	}

	@Override
	public void saveDB() {
		try {
			DbSave.saveAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void noticeHandleTempReward(String uid) {
		TempRewardManager.me().noticeHandleReward(uid);
	}
}

